function creat(cr) {
    return document.createElement(cr);
}
function getid (id) {
    return document.getElementById(id);
}
function getcn (cn) {
    return document.getElementsByClassName(cn);
}
// 给数组排序用的
function sortNumber(a,b){ 
    return a - b;
}

// 加格子
var continer = getid ("continer");
for (var i = 0;i< 10;i++) {   
    var xh = creat("div"); 
    xh.className = "xheng";      
    for (var j = 0;j< 10;j++) {       
        var check = creat("div");
        check.setAttribute('X', i);
        check.setAttribute('Y', j);
        check.setAttribute('state', 1);
        check.className = "check";
        xh.appendChild(check);
    }
    continer.appendChild(xh);
}

var aa = ["#FFFF00","#FFE1FF","#C0FF3E","orange"];
var arr = [];
var num = 0;
var count = 0;
var win;
var check = getcn("check");
for (var i = 0;i < check.length;i++) {
    var ra = parseInt(Math.random()*4);
    check[i].style.background = aa[ra];
}

for (var i = 0;i < check.length;i++) {
    (function (i) {
        check[i].onclick = function () {           
            bb(this);
            count = count + num + 1;
            console.log(arr,count);           
            arr.sort(sortNumber);   //排序
            console.log(arr,count);
            for (var j=arr.length-1;j>=0;j--) {
                console.log(arr[j]);
                check[arr[j]].remove();
            }
// 下面这句记得删除
// for (var j =0;j<arr.length;j++) {check[arr[j]].setAttribute('state', 1);}
            // count = 0;
            arr = [];
            num = 0;
            
        }
    }(i))
}

function bb(cc) {
    win = cc.style.background;
    arr[num] = 10*parseInt(cc.getAttribute("X"))+parseInt(cc.getAttribute("Y"));
    dd(cc);
}
function dd (ee) {          
        ee.setAttribute('state', 0);

        shang(ee);
        xia(ee);
        zuo(ee);
        you(ee);
}

function shang(ee) {
    var x = parseInt(ee.getAttribute("X")) - 1;
    var y = parseInt(ee.getAttribute("Y"));
    if (check[x * 10 + y]) {
        if (parseInt(check[x * 10 + y].getAttribute("state"))) {
            if (check[x * 10 + y].style.background == win) {
                num++;
                arr[num] = x * 10 + y;
                var ff = check[arr[num]];

                dd(ff);
            }
        }
    }
     
}
function xia(ee) {
    var x = parseInt(ee.getAttribute("X")) + 1;
    var y = parseInt(ee.getAttribute("Y"));
    if (check[x * 10 + y]) {
        if (parseInt(check[x * 10 + y].getAttribute("state"))) {
            if (check[x * 10 + y].style.background == win) {
                num++;
                arr[num] = x * 10 + y;
                var ff = check[arr[num]];

                dd(ff);
            }
        }
    }  
}
function zuo(ee) {
    var x = parseInt(ee.getAttribute("X"));
    var y = parseInt(ee.getAttribute("Y")) - 1;
    if (check[x * 10 + y] && y>=0) {
        if (parseInt(check[x * 10 + y].getAttribute("state"))) {
            if (check[x * 10 + y].style.background == win) {
                num++;
                arr[num] = x * 10 + y;
                var ff = check[arr[num]];

                dd(ff);
            }
        }
    }   
}
function you(ee) {
    var x = parseInt(ee.getAttribute("X"));
    var y = parseInt(ee.getAttribute("Y")) + 1;
    if (check[x * 10 + y] && y<10) {
        if (parseInt(check[x * 10 + y].getAttribute("state"))) {
            if (check[x * 10 + y].style.background == win) {
                num++;
                arr[num] = x * 10 + y;
                var ff = check[arr[num]];

                dd(ff);
            }
        }
    }
   
}



